﻿using UnityEngine;
using System.Collections.Generic;
using TTGame;
using CinemaDirector;
using TTGameEngine;

/*
 * BulletTrigger
 * 最简单的子弹触发。每次触发一个子弹。
 *
 * 更多的子弹另外有专门的类去做更细的表现。需要更多的参数控制，如发射间隔、发射轨迹、数量等。
 *
 * 依赖：
 *
 * ！不写重复代码！
 * @author TTun
 * @date 7/29/2017 5:08:34 PM
 */
[CutsceneItemAttribute("Bullet", "LT Bullet Trigger", CutsceneItemGenre.ActorItem)]
public class BulletTrigger : CinemaActorEvent {
    public TargetTypeID m_targetType;
    public MoveStrategy m_moveStrategy = MoveStrategy.Dir_CasterFace;

    //public int AffectObjCount = 0; //可作用人数
    public float m_bulletLifeTime;       //子弹的持续时间
    public string bulletPrefab = "";
    public string shootPosAtObjPos = "";   //发射口位置
    public string muzzlePosName = "";
    [Tooltip("二级子弹Prefab")]
    public string followUpBulletPrefab = "";
    [Tooltip("不造成伤害。注意，依然会碰撞，会触发技能效果")]
    public bool isNoHarm = false;
    [Tooltip("存在多个碰撞对象时，优先选择的策略")]
    public TargetStrategy m_targetStrategy = TargetStrategy.HeroSeatPrior_MonsterNearest;

    List<Puppet> tmpObjs = new List<Puppet>();

    public override void Trigger(GameObject actor) {
#if UNITY_EDITOR
        if(Application.isPlaying == false) {
            return;
        }
#endif
        SkillTL skill = this.GetSkill();
        //fixed:停止技能脚本时，脚本上所有剩余的触发器都会触发一次。
        if(skill.IsStopped()) { return; }

        MagicBall bullet = MagicBallManager.Share().CreateMBWithPrefab(bulletPrefab);
        if(bullet == null) {
            DebugUtil.LogErrorFormat("[BulletTrigger] 子弹预设填的不对. obj:[{0}]", actor.name);
            return;
        }

        Puppet obj = actor.GetComponent<Puppet>();
        if(obj == null) { return; }
        Transform casterTF = actor.transform;
        Transform shootPosTF = obj.GetPosTF(shootPosAtObjPos);
        if(shootPosTF == null) {
            DebugUtil.LogErrorFormat("[BulletTrigger] shootPosTF[{0}] set Error!", shootPosAtObjPos, muzzlePosName);
            return;
        }

        skill.RegistBulletLogicHandler(bullet);
        skill.BulletCount -= bullet.m_bulletConsume;

        bullet.Init(obj, skill.SkillID, skill.SkillCastID, m_bulletLifeTime, m_targetType);
        //兼容不同子弹脚本。
        if(bullet as SimpleBullet) {
            SimpleBullet typeBullet = bullet as SimpleBullet;
            Tools.SetMoveInfoOneTarget(typeBullet.MoveInfo, m_moveStrategy, typeBullet.GetMoveSpeed(), skill, muzzlePosName);
        } else if(bullet as ParabolaBullet) {
            ParabolaBullet typeBullet = bullet as ParabolaBullet;
            ParabolaMoveInfo moveInfo = typeBullet.MoveInfo;
            Tools.SetMoveInfoOneTarget(moveInfo, m_moveStrategy, typeBullet.GetMoveSpeed(), skill, muzzlePosName);
            moveInfo.moveTargetPos = skill.m_selector.ResultHitCenter;
        }

        float fetterAddAttack = 1;
        if(obj.IsHeroOrFriend()) {
            //fetterAddAttack = BattleServerData.Share().FetterAtkRate;
        }
        SkillData skillData = skill.m_skillData;
        bullet.InitDamageInfo(skillData.dmgRatio, skillData.dmgAdd, skillData.crit, fetterAddAttack);
        bullet.SetIsNoHarm(isNoHarm);
        bullet.SetEffectLayerID(obj.GetEffectLayerID());
        bullet.SetMaxColliderCount(skillData.maxHitTarget);
        bullet.SetFollowUpBullet(followUpBulletPrefab, skill.SkillLogicHandler);
        bullet.SetColliderTargetStrategy(m_targetStrategy);
        bullet.AddBuff(skill.SkillLogicHandler.BuffIDs);
        bullet.EmitSelf(shootPosTF.position);
    }

    void OnDrawGizmos() {
        if(Application.isPlaying == false) {
            //Tools.DrawColliderInfo(transform, m_colliderInfo);
        }
    }

}
